<?php

/**
 * 评论类
 * 
 * @author ShuangYa
 * @package Blog
 * @category Library
 * @link http://www.sylingd.com/
 * @copyright Copyright (c) 2015 ShuangYa
 * @license http://lab.sylingd.com/go.php?name=blog&type=license
 */

namespace blog\libs;
use \Sy;
use \sy\lib\YHtml;
use \sy\lib\db\YMysql;
use \sy\base\SYException;
use \blog\libs\BArticle;
use \blog\libs\option;

class BComment {
	/**
	 * 获取一条评论
	 * @access public
	 * @param int $id
	 * @return array
	 */
	public static function get($id) {
		return YMysql::i()->getOne("SELECT * FROM `#@__comment` WHERE id = ?", [$id]);
	}
	/**
	 * 增加一条评论
	 * @access public
	 * @param int $id 文章ID
	 * @param string $body
	 * @param int $time
	 * @param array $author 评论者信息，包括name,mail,ip,ua
	 * @return int
	 */
	public static function add($id, $body, $time, $author) {
		$db = YMysql::i();
		extract($author);
		//插入
		$body = addslashes($body);
		$name = addslashes($name);
		$avatar = md5($mail);
		$mail = addslashes($mail);
		$ip = addslashes($ip);
		$ua = addslashes($ua);
		$sql = "INSERT INTO `#@__comment`(`aid`,`author`,`mail`,`avatar`,`body`,`publish`,`ip`,`ua`) VALUES ('$title','$tags','$time','$time','$body')";
		$db->query('add', $sql);
		$id = $db->getLastId();
		return $id;
	}
	/**
	 * 删除一条评论
	 * @access public
	 * @param int $id
	 */
	public static function del($id) {
		$article = static::get($id);
		$title = $article->title;
		if (empty($title)) {
			return;
		}
		$tags = explode(',', $article->tags);
		foreach ($tags as $t) {
			YMysql::i()->query('del', "UPDATE `#@__meta` SET num = num-1 WHERE title = ?", [$t]);
		}
		YMysql::i()->query('del', 'DELETE FROM `#@__relation` WHERE aid = ?', [$id]);
		YMysql::i()->query('del', 'DELETE FROM `#@__article` WHERE id = ?', [$id]);
	}
	/**
	 * 获取列表
	 * @access public
	 * @param array $param
	 * @param mixed $param[body] 是否返回文章内容
	 * @param mixed $param[find] 筛选方式
	 * @param string $param[id/tag] 筛选条件
	 * @param string $param[limit] 数量，SQL形式(0,30)
	 * @return Object(ArticleList_Action)
	 */
	public static function getList($param = []) {
		extract($param);
		if ($body) {
			if (isset($find)) {
				$row = 'a.*';
			} else {
				$row = '*';
			}
		} else {
			if (isset($find)) {
				$row = 'a.id,a.title,a.publish,a.modify,a.tags';
			} else {
				$row = 'id,title,publish,modify,tags';
			}
		}
		if ($find === 'id') {
			$sql = 'SELECT b.mid,' . $row . ' FROM `#@__article` a,`#@__relation` b WHERE a.id = b.aid AND b.mid = ? ORDER BY a.publish DESC ';
			$data = [$id];
		} elseif ($find === 'tag') {
			$sql = 'SELECT b.mid,' . $row . ' FROM `#@__article` a,`#@__relation` b,`#@__meta` c WHERE a.id = b.aid AND b.mid = c.id AND c.title = ? ORDER BY a.publish DESC ';
			$data = [$tag];
		} else {
			$sql = 'SELECT ' . $row . ' FROM `#@__article` ORDER BY publish DESC ';
			$data = [];
		}
		if (isset($limit)) {
			$sql .= 'LIMIT ' . $limit;
		}
		YMysql::i()->query('list', $sql, $data);
		$r = YMysql::i()->getAll('list');
		$num = static::getNum($param);
		return new ArticleList_Action($r, $num, $param);
	}
	/**
	 * 获取满足条件的文章数量
	 * @access public
	 * @param array $param
	 * @param mixed $param[find] 筛选方式
	 * @param string $param[id/tag] 筛选条件
	 * @return int
	 */
	public static function getNum($param = []) {
		if ($param['find'] === 'id') {
			$sql = 'SELECT count(*) as num FROM `#@__article` a,`#@__relation` b WHERE a.id = b.aid AND b.mid = ?';
			$data = [$param['id']];
		} elseif ($param['find'] === 'tag') {
			$sql = 'SELECT count(*) as num FROM `#@__article` a,`#@__relation` b,`#@__meta` c WHERE a.id = b.aid AND b.mid = c.id AND c.title = ?';
			$data = [$param['tag']];
		} else {
			$sql = 'SELECT count(*) as num FROM `#@__article`';
			$data = [];
		}
		$r = YMysql::i()->getOne($sql, $data);
		return $r['num'];
	}
	/**
	 * 发送Ping通知和百度主动推送
	 * @access public
	 * @param int $id 文章ID
	 */
	public static function sendPing($id) {
		if (!class_exists('IXR_Client', FALSE)) {
			require(Sy::$appDir . 'third_party/IXR_Library.php');
		}
		$servers = explode("\n", option::get('seoPing'));
		foreach ($servers as $server) {
			$client = new IXR_Client($server);
			$client->timeout = 3; //较短的超时时间
			$client->useragent .= ' -- SYBlog/' . \blog\BVER;
			$client->query('weblogUpdates.extendedPing', option::get('sitename'), Sy::createUrl(), Sy::createUrl(['article/view'. 'id' => $id]), Sy::createUrl('feed/rss', 'xml'));
		}
		//百度主动推送
		$baiduSubmit = unserialize(option::get('seoBaiduSubmit'));
		if ($baiduSubmit['enable']) {
			\blog\curl_get('http://data.zz.baidu.com/urls?' . http_build_query(['site' => $baiduSubmit['site'], 'token' => $baiduSubmit['token']]), ['header' => ['Content-Type: text/plain'], 'post' => Sy::createUrl(['article/view'. 'id' => $id])]);
		}
	}
	/**
	 * 生成静态
	 * @access public
	 */
	public static function mkStatic($id) {
		if (option::get('static') == 1) {
			\blog\libs\MkHtml::mkArticle($id);
			\blog\libs\MkHtml::mkFeed('rss');
			\blog\libs\MkHtml::mkFeed('rdf');
			\blog\libs\MkHtml::mkFeed('atom');
		}
	}
}